<?php
namespace app\app\controller;
use think\Db;
class Oneself extends Common
{
    //检查更新
    public function check_update()
    {
        $this_version = req('version');
        $type = req('type');
        $msg['state'] = 0;
        $msg['data'] = null;
        $update_config = Db::table('update')->field('url,size,version,description')->where('type',$type)->order('time desc')->find();
        if(count($update_config) == 0){
            $msg['data']['url'] = '';
            $msg['data']['size'] = '';
            $msg['data']['version'] = '';
            $msg['data']['description'] = '';
            $msg['msg'] = '当前已是最新版本';
        }else{
            if(str_replace('.','',$this_version) < str_replace('.','',$update_config['version'])){
                $msg['state'] = 1;
                $msg['msg'] = '检测到新版本，请更新';
                $msg['data'] = $update_config;
            }else{
                $msg['msg'] = '当前已是最新版本';
            }
        }
        return json($msg);
    }

    //修改密码
    public function edit_pwd()
    {
        $uid = token_decode(req('token'));
        $old_pwd = md5(req('old_pwd'));
        $new_pwd = md5(req('new_pwd'));
        $table = req('type')?req('type'):'user';
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($old_pwd) || empty($new_pwd)) {
            $msg['msg'] = '参数错误';
        } else {
            $user = Db::table($table)->where('id',$uid)->find();
            if($user['password'] != $old_pwd){
                $msg['msg'] = '原密码输入错误';
            }else{
                $msg['state'] = 1;
                Db::table($table)->where('id',$uid)->update(['password'=>$new_pwd]);
                $msg['msg'] = '修改成功';
            }
        }
        return json($msg);
    }

    //意见反馈
    public function send_opinion()
    {
        $uid = token_decode(req('token'));
        $content = req('content');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        } else {
            $area['content'] = $content;
            $area['time'] = time();
            Db::table('opinion')->insert($area);
            $msg['state'] = 1;
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }

    //获取消息列表
    public function message_list()
    {
        $uid = token_decode(req('token'));
        $type = req('type');
        $page = !empty(req('page')) ? req('page') : 1;
        $page_size = !empty(req('pagesize')) ? req('page_size') : 10;
        $data = Db::table('message')->field('title,message,time,type,parameters')->where(['uid'=>$uid,'account_type'=>$type])->page($page,$page_size)->select();
        return json(['state'=>1,'data'=>$data,'msg'=>'获取成功']);
    }
}